package net.hymnal.church;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SearchResultActivity extends Activity{
	String[] array = null;
	ListView mList;
	OnItemClickListener listener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View view, int position,
				long id) {
			int numSelected = -1;
			try {
				String str = array[(int) id];
				int index = str.indexOf(" :");
				if (index >= 0) {
					str = str.substring(0, index);
					numSelected = Integer.decode(str) - 1;
				} else {
					numSelected = Integer.decode(str) - 1;
				}
				HymnActivity.setSelectedHymn(numSelected);
				Intent myIntent = new Intent(view.getContext(),
						HymnActivity.class);
				startActivityForResult(myIntent, 0);

			} catch (NumberFormatException nfe) {
				Log.e("Bad number format.", nfe.getMessage());
			}
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	setTheme(GimnsMain.mSettings.getCurrentTheme());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_search_result);
        array = HymnsManager.getSelectedArray();        
		mList = (ListView) findViewById(R.id.listResultSearch);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1, array);

		mList.setAdapter(adapter);
		mList.setOnItemClickListener(listener);
	}
}
